﻿//-------------------------------------------------------------------------------------------------
//   Copyright (c) 2004, Outercurve Foundation.
//   This file is released under the Microsoft Reciprocal License (MS-RL).
//   See http://wix.codeplex.com/SourceControl/latest#src/ext/TagExtension/wixext/TagBinder.cs
//-------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace SwidHelperLib
{
    public partial class SwidHelper
    {
        private static string NEGATIVE_NUMBER = "-1";
        private static string ZERO = "0";

        private enum TagType
        {
            Unknown,
            Application,
            Component,
            Feature,
            Group,
            Patch,
        }

        private class SoftwareTag
        {
            public string Regid { get; set; }
            public string Id { get; set; }
            public TagType Type { get; set; }
        }

        private static void CreateTagFile(
            Stream stream, 
            string regid, 
            string uniqueId, 
            string name, 
            Version version, 
            string manufacturer, 
            bool licensed, 
            TagType tagType,
            IEnumerable<string> files,
            IList<SoftwareTag> containedTags)
        {
            using (XmlTextWriter writer = new XmlTextWriter(stream, Encoding.UTF8))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("software_identification_tag", "http://standards.iso.org/iso/19770/-2/2009/schema.xsd");
                writer.WriteElementString("entitlement_required_indicator", licensed ? "true" : "false");

                writer.WriteElementString("product_title", name);

                writer.WriteStartElement("product_version");
                writer.WriteElementString("name", version.ToString());
                writer.WriteStartElement("numeric");
                writer.WriteElementString("major", version.Major.ToString());
                writer.WriteElementString("minor", version.Minor.ToString());

                string convertedSubVersion = version.Build.ToString();
                writer.WriteElementString( "build",
                    convertedSubVersion == SwidHelper.NEGATIVE_NUMBER ? SwidHelper.ZERO : convertedSubVersion );

                convertedSubVersion = version.Revision.ToString();
                writer.WriteElementString( "review",
                    convertedSubVersion == SwidHelper.NEGATIVE_NUMBER ? SwidHelper.ZERO : convertedSubVersion );

                writer.WriteEndElement();
                writer.WriteEndElement();

                writer.WriteStartElement("software_creator");
                writer.WriteElementString("name", manufacturer);
                writer.WriteElementString("regid", regid);
                writer.WriteEndElement();

                if (licensed)
                {
                    writer.WriteStartElement("software_licensor");
                    writer.WriteElementString("name", manufacturer);
                    writer.WriteElementString("regid", regid);
                    writer.WriteEndElement();
                }

                writer.WriteStartElement("software_id");
                writer.WriteElementString("unique_id", uniqueId);
                writer.WriteElementString("tag_creator_regid", regid);
                writer.WriteEndElement();

                writer.WriteStartElement("tag_creator");
                writer.WriteElementString("name", manufacturer);
                writer.WriteElementString("regid", regid);
                writer.WriteEndElement();

                if (null != containedTags && 0 < containedTags.Count)
                {
                    writer.WriteStartElement("complex_of");
                    foreach (SoftwareTag tag in containedTags)
                    {
                        writer.WriteStartElement("software_id");
                        writer.WriteElementString("unique_id", tag.Id);
                        writer.WriteElementString("tag_creator_regid", tag.Regid);
                        writer.WriteEndElement(); // </software_id>
                    }
                    writer.WriteEndElement(); // </complex_of>
                }

                if ( files != null )
                {
                    writer.WriteStartElement( "files" );
                    foreach ( string file in files )
                    {
                        writer.WriteElementString( "file", file );
                    }

                    writer.WriteEndElement(); // </files>
                }

                if (TagType.Unknown != tagType)
                {
                    writer.WriteStartElement("extended_information");
                    writer.WriteStartElement("tag_type", "http://www.tagvault.org/tv_extensions.xsd");
                    writer.WriteValue(tagType.ToString());
                    writer.WriteEndElement(); // </tag_type>
                    writer.WriteEndElement(); // </extended_information>
                }

                writer.WriteEndElement(); // </software_identification_tag>
            }
        }
    }
}
